package com.chrisevansdev.irisysaudit;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chrisevansdev.widget.NumberPicker;
import com.chrisevansdev.widget.NumberPicker.OnChangedListener;

public class CheckoutManager extends BaseAdapter implements OnChangedListener, Serializable
{
	private static final long serialVersionUID = 2794811960095192070L;

	public class Checkout implements Serializable
	{
		private static final long serialVersionUID = -159798791735105642L;
		private String mName;
		private int mCount;	
		
		public Checkout(String name)
		{
			setName(name);
			setCount(0);
		}
		
		public Checkout(String name, Integer count)
		{
			setName(name);
			setCount(count);
		}
		
		public String getName()
		{
			return mName;
		}
		
		public int getCount()
		{
			return mCount;
		}
		
		public void setName(String name)
		{
			mName = name;
		}
	
		public void setCount(int count)
		{
			mCount = count;
		}
	}
	
    private Context mContext;
    private ArrayList<Checkout> mCheckoutArray = new ArrayList<Checkout>();
    private ArrayList<UpdateListener> mUdateListeners = new ArrayList<UpdateListener>();
        
    public interface UpdateListener {
        void onUpdate(int id, String name, int count);
    }
   
    public CheckoutManager(Context c) 
    {
        mContext = c;
        mCheckoutArray.clear();
        mUdateListeners.clear();
    }

    public void AddCheckout(String strName)
    {
    	mCheckoutArray.add(new Checkout(strName));
    }
    
    public void AddCheckout(String strName, Integer nCount)
    {
    	mCheckoutArray.add(new Checkout(strName, nCount));
    }
    
    public ArrayList<Checkout> getCheckoutArray()
    {
    	// need to figure out how to make this return a const
    	return mCheckoutArray;
    }
    
    @Override
    public int getCount() 
    {
        return mCheckoutArray.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return mCheckoutArray.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }
   
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    	LinearLayout checkout;
    		    	
        if (convertView == null) 
        {          	        	
        	// if it's not recycled, initialize some attributes
        	checkout = new LinearLayout(mContext);
    		checkout.setOrientation(LinearLayout.VERTICAL);
    				
    		// build text title for the checkout
    		TextView checkoutName = new TextView(mContext);
    		checkoutName.setText(mCheckoutArray.get(position).getName());
    		checkoutName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
    		checkoutName.setGravity(Gravity.CENTER_HORIZONTAL);   
    		checkout.addView(checkoutName);
    		
    		// build the numpicker for this checkout
    		NumberPicker numPicker = new NumberPicker(mContext); 
    		numPicker.setId(position);
    		numPicker.setOnChangeListener(this);
      		
    		Checkout c = mCheckoutArray.get(position);
    		numPicker.setCurrent(c.getCount());
    		
    		checkout.addView(numPicker);	
        } 
        else 
        {
        	// we're recycling the checkout group so change the attributes
            checkout = (LinearLayout) convertView;

            TextView checkoutName = (TextView) checkout.getChildAt(0);
            checkoutName.setText(mCheckoutArray.get(position).getName());
    		checkoutName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
    		checkoutName.setGravity(Gravity.CENTER_HORIZONTAL);   
       
    		NumberPicker numPicker = (NumberPicker) checkout.getChildAt(1); 
            numPicker.setId(position);
    		numPicker.setOnChangeListener(this);
    		
    		Checkout c = mCheckoutArray.get(position);
    		numPicker.setCurrent(c.getCount());
    	}
        
  		return checkout;
    }
    
    @Override
	public void onChanged(NumberPicker picker, int oldVal, int newVal) 
	{
    	Checkout c = mCheckoutArray.get(picker.getId());
   
    	c.setCount(newVal);
    	
    	// Notify all update listeners
    	for (UpdateListener ul : mUdateListeners)
    	{
    		ul.onUpdate(picker.getId(), c.getName(), c.getCount());
    	}
    	
	}

    public void AddUpdateListener(UpdateListener listener)
    {
    	mUdateListeners.add(listener);
    }
    
    public void RemoveUpdateListener(UpdateListener listener)
    {
    	mUdateListeners.remove(listener);
    }
}
